// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var leftMenu = require("../leftMenu/leftMenu");
	var topMenu = require("../topMenu/topMenu");
	var footer = require("../footer/footer_main");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var tools = require("tools");
	var databind = require("databind");
	var css_insurance_detail = require("./insuranceDetail.css");
	var html_select_insurance = require("./html_select_insurance.html");
	var html_oppose_insurance = require("./html_oppose_insurance.html");
	var html_revoke_insurance = require("./html_revoke_insurance.html");
	var js_insuranceManage = require("./insuranceManage.js");
	var financeId;
	var insuranceId;
	var insuranceState;
	var insuranceManage = require("./insuranceManage");
	var init = function(page) {
		financeId = page.params.financeId;
		insuranceId = page.params.insuranceId;
//		footer.init();
		$("#footer").removeClass("none");
//		leftMenu.init(8);
//		topMenu.init(6);
		router.init();
//		$(".table-section").removeClass("none");
//		$(".footer").removeClass("none");
		tools.loadList("inquireInsuranceDetail.json", $("#insuranceDetailBody"), $("#insuranceDetailTpl"), {
			insuranceId: insuranceId
		}, function(data) {
			insuranceState = $("#insuranceState").val();

			if (insuranceState == 1) {
				$("#insuranceDetail .topButton").html('<a id="insranceUpdateButtn">修改</a><a id="opposeInsurance">审核</a>');
			} else if (insuranceState == 2) {
				$("#insuranceDetail .topButton").html('<a id="revokeInsurance">撤销</a>');
			} else if (insuranceState == 3) {
				$("#insuranceDetail .topButton").html("");
			}
			
			//修改保险信息
			$("#insranceUpdateButtn").unbind("click").click(function(){
				router.route("insuranceManage/createInsurance.html?financeId=" + financeId + "&insuranceId=" + insuranceId);
			});
			
			//同意保险信息
			$("#consentInsurance").unbind("click").click(function(){
				dialog.showConfirmDialog("确认同意保险", "确认同意该融资保险？", function() {
					consentInsurance(insuranceId);
				});
			});
			
			//拒绝保险信息
			$("#opposeInsurance").unbind("click").click(function() {
				$("#popdialog").html(html_oppose_insurance);
				dialog.showDialog($("#oppose_insurance_dialog"));
				databind.fillForm($("#oppose_insurance_dialog"), {
					insuranceId: insuranceId
				});
				tools.requireInput($("#oppose_insurance_dialog"));
				$(".sure_yes").unbind("click").click(function() {
					databind.submitDom($("#oppose_insurance_dialog"), function() {
						base.alert(1, "审核成功！");
						dialog.hideDialog($("#oppose_insurance_dialog"));
						router.route("insuranceManage/insuranceDetail.html?financeId=" + financeId + "&insuranceId=" + insuranceId);
					});
				});
			});

			$("#insuranceDetail .insuranceState").val($("#insuranceDetail #insuranceState").val());
			$("#insuranceDetail .mold").val(data.datas[0].mold);
			$("#insuranceDetail .purchase").val(data.datas[0].purchase);

			var fs = $("#insuranceFileUuidDetail").val();
			$("#insuranceDetail .fs").html("");
			$.each(fs.split(','), function() {
				var _this = this;
				if (_this != "") {
					var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + _this + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + _this + '" class="showProductIntro">' + tools.inquireFile(_this) + '</a></div>'
					$("#insuranceDetail .fs").append(str);
				}
			});

			var am = 0;
			$.each($(".premium"), function() {
				am += $(this).html() * 1;
			});
			$(".premiums").val(am);

			//险种明细
			$("#insuranceDetail").on("click", ".insuranceCoverageDetail", function() {
				var insuranceCoverageId = $(this).parents("tr").attr("insuranceCoverageId");
				$("#popdialog").html(html_select_insurance);
				dialog.showDialog($("#select_insurance_dialog"));
				tools.loadList("inquireInsuranceCoverageDetail.json", $("#select_insurance_dialog .dialog_content"), $("#dialog_content_detail"), {
					insuranceCoverageId: insuranceCoverageId
				}, function() {
					$("#select_insurance_dialog").on("click", ".sure_yes", function() {
						dialog.hideDialog($("#select_insurance_dialog"));
					});
					$("#select_insurance_dialog .dialogTitle").html("险种详情");
					$("#select_insurance_dialog .sure_no").remove();
					databind.disableInput($("#select_insurance_dialog"));
				});
			});
			tools.requireInput($("#insuranceDetail"));
		});
		databind.disableInput($("#insuranceDetail"));
		//		js_insuranceManage.init();

		$("#insuranceDetail .applicationDetail").unbind("click").click(function() {
			router.route("financing/financingDetailInfo.html?financeId=" + $(this).attr("financeId"));
		});
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			location.href = "../login/login.html";
		}

		$("#leftMenu9").on("click", ".claimManage", function() {
			router.route("claimManage/claimManage.html");
		});

		$("#leftMenu9").on("click", ".insurance1", function() {
			location.href = "../insuranceManage/insuranceManage.html?insuranceState=1";
		});

		$("#leftMenu9").on("click", ".insurance2", function() {
			location.href = "../insuranceManage/insuranceManage.html?insuranceState=2";
		});

		$("#leftMenu9").on("click", ".insurance3", function() {
			location.href = "../insuranceManage/insuranceManage.html?insuranceState=3";
		});

		//撤销保险信息
		$("#insuranceDetail").on("click", "#revokeInsurance", function() {
			$("#popdialog").html(html_revoke_insurance);
			dialog.showDialog($("#revoke_insurance_dialog"));
			databind.fillForm($("#revoke_insurance_dialog"), {
				insuranceId: insuranceId
			});
			tools.requireInput($("#revoke_insurance_dialog"));
			tools.inquireCommonSelectorList(9, $("#revoke_insurance_dialog .overReson"));
			$(".sure_yes").unbind("click").click(function() {
				databind.submitDom($("#revoke_insurance_dialog"), function() {
					base.alert(1, "撤销成功！");
					dialog.hideDialog($("#revoke_insurance_dialog"));
					router.route("insuranceManage/insuranceDetail.html?financeId=" + financeId + "&insuranceId=" + insuranceId);
				});
			});

			$(".sure_no").unbind("click").click(function() {
				dialog.hideDialog($("#oppose_insurance_dialog"));
			});
		});
	};

	//上传文件
	var uploadFile = function() {
		if ($("#insuranceAttach").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'insuranceAttach',
				dataType: 'json',
				success: function(data) {
					var str = '<div class="showProductIntroDiv"><a target="_blank" class="mas" fileUuid="' + data.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.data[0].fileUuid + '" class="showProductIntro">' + data.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="deleteProductIntro">x</a></div>'
					$(".insuranceAttach").before(str);
				},
				complete: function(xml) {
					$("#insuranceAttach").change(function() {
						uploadFile();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			base.alert(3, "请选择文件！");
		}
	};

	//同意融资保险
	var consentInsurance = function(insuranceId) {
		base.fnAjax({
			action: "consentInsurance.json",
			oData: {
				insuranceId: insuranceId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "同意融资保险成功！");
					router.route("insuranceManage/insuranceDetail.html?financeId=" + financeId + "&insuranceId=" + insuranceId);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});